{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4e7f72f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0814af24",
   "metadata": {},
   "source": [
    "# pandas时间序列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e89fbadd",
   "metadata": {},
   "source": [
    "**时间戳**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e7337922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2030-02-13', '2030-02-14', '2030-02-15', '2030-02-16'], dtype='datetime64[ns]', freq='D')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['2030-02-13', '2030-02-14', '2030-02-15', '2030-02-16'], dtype='period[D]')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 创建一个时间戳\n",
    "pandas.Timestamp('2030-2-3') # 时刻数据\n",
    "pandas.Period('2030-2-3',freq='D') # 时期数据\n",
    "# freq  Y:年  M:月  D:日\n",
    "\n",
    "# 批量生成时刻数据\n",
    "index1 = pandas.date_range(start='2030.02.13',periods=4,freq='D') # period 表示要创建多少个时间\n",
    "\n",
    "# 批量生成时期数据\n",
    "index2 = pandas.period_range(start='2030.02.13',periods=4,freq='D')\n",
    "\n",
    "display(index1,index2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c977984",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6e8b6534",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-22 23:41:18.987000'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换方法\n",
    "pandas.to_datetime(['2030.03.14',])\n",
    "\n",
    "# 时间戳 -> 时间\n",
    "pandas.to_datetime([1899678987],unit='s')\n",
    "\n",
    "pandas.to_datetime([1899678987],unit='ms')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f982a51a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-23 23:41:18.987000'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dt = pandas.to_datetime([1899678987],unit='ms')\n",
    "dt + pandas.DateOffset(hours=8)\n",
    "dt + pandas.DateOffset(days=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33ad67be",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0ac7f8b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pandas.date_range(start='2030.3.20',periods=100,freq='D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "11d0591e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-20     0\n",
       "2030-03-21     1\n",
       "2030-03-22     2\n",
       "2030-03-23     3\n",
       "2030-03-24     4\n",
       "              ..\n",
       "2030-06-23    95\n",
       "2030-06-24    96\n",
       "2030-06-25    97\n",
       "2030-06-26    98\n",
       "2030-06-27    99\n",
       "Freq: D, Length: 100, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts = pandas.Series(range(len(index)),index=index)\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "0e4384c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-20     0\n",
       "2030-03-21     1\n",
       "2030-03-22     2\n",
       "2030-03-23     3\n",
       "2030-03-24     4\n",
       "2030-03-25     5\n",
       "2030-03-26     6\n",
       "2030-03-27     7\n",
       "2030-03-28     8\n",
       "2030-03-29     9\n",
       "2030-03-30    10\n",
       "2030-03-31    11\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引\n",
    "ts['2030.3.23'] # 日\n",
    "ts['2030.3'] # 3月份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "fa5ebcb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-23     3\n",
       "2030-03-24     4\n",
       "2030-03-25     5\n",
       "2030-03-26     6\n",
       "2030-03-27     7\n",
       "2030-03-28     8\n",
       "2030-03-29     9\n",
       "2030-03-30    10\n",
       "2030-03-31    11\n",
       "2030-04-01    12\n",
       "2030-04-02    13\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "ts['2030.3.23':'2030.4.2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "7b10e753",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,\n",
       "       5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0,\n",
       "       1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3,\n",
       "       4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6,\n",
       "       0, 1, 2, 3],\n",
       "      dtype='int32')"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 属性\n",
    "ts.index\n",
    "ts.index.year # 取到所有的年\n",
    "ts.index.dayofweek # 星期几"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68c65840",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1834d7b",
   "metadata": {},
   "source": [
    "**时间序列中常用的方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "cd83275e",
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pandas.date_range('2030.3.1 00:00',periods=365,freq='D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "170758b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01     99\n",
       "2030-03-02     66\n",
       "2030-03-03    216\n",
       "2030-03-04    177\n",
       "2030-03-05    227\n",
       "             ... \n",
       "2031-02-24    397\n",
       "2031-02-25    260\n",
       "2031-02-26    121\n",
       "2031-02-27     78\n",
       "2031-02-28     93\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ts = pandas.Series(numpy.random.randint(0,500,len(index)),index=index)\n",
    "display(ts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "f3930b68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01      NaN\n",
       "2030-03-02      NaN\n",
       "2030-03-03      NaN\n",
       "2030-03-04     99.0\n",
       "2030-03-05     66.0\n",
       "              ...  \n",
       "2031-02-24      2.0\n",
       "2031-02-25    428.0\n",
       "2031-02-26     74.0\n",
       "2031-02-27    397.0\n",
       "2031-02-28    260.0\n",
       "Freq: D, Length: 365, dtype: float64"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 移动\n",
    "ts.shift() # 默认往后移动1位\n",
    "ts.shift(periods=3) # 往后移动3位，如果想往前移就填负数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "2d93d288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01     99\n",
       "2030-04-01    117\n",
       "2030-05-01    132\n",
       "2030-06-01     76\n",
       "2030-07-01      0\n",
       "2030-08-01    202\n",
       "2030-09-01    456\n",
       "2030-10-01    209\n",
       "2030-11-01    387\n",
       "2030-12-01    240\n",
       "2031-01-01     16\n",
       "2031-02-01    413\n",
       "Freq: MS, dtype: int32"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 频率的转换\n",
    "ts.asfreq(pandas.tseries.offsets.Week()) # 按星期变化，原来是按照天变化\n",
    "ts.asfreq(pandas.tseries.offsets.MonthBegin()) # 这里按月变化，从月的第一天开始"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4294eff6",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "974cdc36",
   "metadata": {},
   "source": [
    "**重采样**\n",
    "- resample：根据日期维度进行数据聚合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "e3d0559f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-31     7176\n",
       "2030-05-31    13929\n",
       "2030-07-31    14346\n",
       "2030-09-30    15369\n",
       "2030-11-30    15543\n",
       "2031-01-31    16787\n",
       "2031-03-31     5556\n",
       "Freq: 2M, dtype: int32"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重采样，resample，分钟T  小时H  日D  周W  月M  年Y\n",
    "ts.resample(rule='2M').sum() # 以每2个月为单位进行汇总"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8caec7cf",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a4b0165",
   "metadata": {},
   "source": [
    "**时区**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "937fd38e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# tz就是timezone的意思，时区的意思\n",
    "import pytz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "38063a6e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Africa/Abidjan',\n",
       " 'Africa/Accra',\n",
       " 'Africa/Addis_Ababa',\n",
       " 'Africa/Algiers',\n",
       " 'Africa/Asmara',\n",
       " 'Africa/Bamako',\n",
       " 'Africa/Bangui',\n",
       " 'Africa/Banjul',\n",
       " 'Africa/Bissau',\n",
       " 'Africa/Blantyre',\n",
       " 'Africa/Brazzaville',\n",
       " 'Africa/Bujumbura',\n",
       " 'Africa/Cairo',\n",
       " 'Africa/Casablanca',\n",
       " 'Africa/Ceuta',\n",
       " 'Africa/Conakry',\n",
       " 'Africa/Dakar',\n",
       " 'Africa/Dar_es_Salaam',\n",
       " 'Africa/Djibouti',\n",
       " 'Africa/Douala',\n",
       " 'Africa/El_Aaiun',\n",
       " 'Africa/Freetown',\n",
       " 'Africa/Gaborone',\n",
       " 'Africa/Harare',\n",
       " 'Africa/Johannesburg',\n",
       " 'Africa/Juba',\n",
       " 'Africa/Kampala',\n",
       " 'Africa/Khartoum',\n",
       " 'Africa/Kigali',\n",
       " 'Africa/Kinshasa',\n",
       " 'Africa/Lagos',\n",
       " 'Africa/Libreville',\n",
       " 'Africa/Lome',\n",
       " 'Africa/Luanda',\n",
       " 'Africa/Lubumbashi',\n",
       " 'Africa/Lusaka',\n",
       " 'Africa/Malabo',\n",
       " 'Africa/Maputo',\n",
       " 'Africa/Maseru',\n",
       " 'Africa/Mbabane',\n",
       " 'Africa/Mogadishu',\n",
       " 'Africa/Monrovia',\n",
       " 'Africa/Nairobi',\n",
       " 'Africa/Ndjamena',\n",
       " 'Africa/Niamey',\n",
       " 'Africa/Nouakchott',\n",
       " 'Africa/Ouagadougou',\n",
       " 'Africa/Porto-Novo',\n",
       " 'Africa/Sao_Tome',\n",
       " 'Africa/Tripoli',\n",
       " 'Africa/Tunis',\n",
       " 'Africa/Windhoek',\n",
       " 'America/Adak',\n",
       " 'America/Anchorage',\n",
       " 'America/Anguilla',\n",
       " 'America/Antigua',\n",
       " 'America/Araguaina',\n",
       " 'America/Argentina/Buenos_Aires',\n",
       " 'America/Argentina/Catamarca',\n",
       " 'America/Argentina/Cordoba',\n",
       " 'America/Argentina/Jujuy',\n",
       " 'America/Argentina/La_Rioja',\n",
       " 'America/Argentina/Mendoza',\n",
       " 'America/Argentina/Rio_Gallegos',\n",
       " 'America/Argentina/Salta',\n",
       " 'America/Argentina/San_Juan',\n",
       " 'America/Argentina/San_Luis',\n",
       " 'America/Argentina/Tucuman',\n",
       " 'America/Argentina/Ushuaia',\n",
       " 'America/Aruba',\n",
       " 'America/Asuncion',\n",
       " 'America/Atikokan',\n",
       " 'America/Bahia',\n",
       " 'America/Bahia_Banderas',\n",
       " 'America/Barbados',\n",
       " 'America/Belem',\n",
       " 'America/Belize',\n",
       " 'America/Blanc-Sablon',\n",
       " 'America/Boa_Vista',\n",
       " 'America/Bogota',\n",
       " 'America/Boise',\n",
       " 'America/Cambridge_Bay',\n",
       " 'America/Campo_Grande',\n",
       " 'America/Cancun',\n",
       " 'America/Caracas',\n",
       " 'America/Cayenne',\n",
       " 'America/Cayman',\n",
       " 'America/Chicago',\n",
       " 'America/Chihuahua',\n",
       " 'America/Ciudad_Juarez',\n",
       " 'America/Costa_Rica',\n",
       " 'America/Creston',\n",
       " 'America/Cuiaba',\n",
       " 'America/Curacao',\n",
       " 'America/Danmarkshavn',\n",
       " 'America/Dawson',\n",
       " 'America/Dawson_Creek',\n",
       " 'America/Denver',\n",
       " 'America/Detroit',\n",
       " 'America/Dominica',\n",
       " 'America/Edmonton',\n",
       " 'America/Eirunepe',\n",
       " 'America/El_Salvador',\n",
       " 'America/Fort_Nelson',\n",
       " 'America/Fortaleza',\n",
       " 'America/Glace_Bay',\n",
       " 'America/Goose_Bay',\n",
       " 'America/Grand_Turk',\n",
       " 'America/Grenada',\n",
       " 'America/Guadeloupe',\n",
       " 'America/Guatemala',\n",
       " 'America/Guayaquil',\n",
       " 'America/Guyana',\n",
       " 'America/Halifax',\n",
       " 'America/Havana',\n",
       " 'America/Hermosillo',\n",
       " 'America/Indiana/Indianapolis',\n",
       " 'America/Indiana/Knox',\n",
       " 'America/Indiana/Marengo',\n",
       " 'America/Indiana/Petersburg',\n",
       " 'America/Indiana/Tell_City',\n",
       " 'America/Indiana/Vevay',\n",
       " 'America/Indiana/Vincennes',\n",
       " 'America/Indiana/Winamac',\n",
       " 'America/Inuvik',\n",
       " 'America/Iqaluit',\n",
       " 'America/Jamaica',\n",
       " 'America/Juneau',\n",
       " 'America/Kentucky/Louisville',\n",
       " 'America/Kentucky/Monticello',\n",
       " 'America/Kralendijk',\n",
       " 'America/La_Paz',\n",
       " 'America/Lima',\n",
       " 'America/Los_Angeles',\n",
       " 'America/Lower_Princes',\n",
       " 'America/Maceio',\n",
       " 'America/Managua',\n",
       " 'America/Manaus',\n",
       " 'America/Marigot',\n",
       " 'America/Martinique',\n",
       " 'America/Matamoros',\n",
       " 'America/Mazatlan',\n",
       " 'America/Menominee',\n",
       " 'America/Merida',\n",
       " 'America/Metlakatla',\n",
       " 'America/Mexico_City',\n",
       " 'America/Miquelon',\n",
       " 'America/Moncton',\n",
       " 'America/Monterrey',\n",
       " 'America/Montevideo',\n",
       " 'America/Montserrat',\n",
       " 'America/Nassau',\n",
       " 'America/New_York',\n",
       " 'America/Nome',\n",
       " 'America/Noronha',\n",
       " 'America/North_Dakota/Beulah',\n",
       " 'America/North_Dakota/Center',\n",
       " 'America/North_Dakota/New_Salem',\n",
       " 'America/Nuuk',\n",
       " 'America/Ojinaga',\n",
       " 'America/Panama',\n",
       " 'America/Paramaribo',\n",
       " 'America/Phoenix',\n",
       " 'America/Port-au-Prince',\n",
       " 'America/Port_of_Spain',\n",
       " 'America/Porto_Velho',\n",
       " 'America/Puerto_Rico',\n",
       " 'America/Punta_Arenas',\n",
       " 'America/Rankin_Inlet',\n",
       " 'America/Recife',\n",
       " 'America/Regina',\n",
       " 'America/Resolute',\n",
       " 'America/Rio_Branco',\n",
       " 'America/Santarem',\n",
       " 'America/Santiago',\n",
       " 'America/Santo_Domingo',\n",
       " 'America/Sao_Paulo',\n",
       " 'America/Scoresbysund',\n",
       " 'America/Sitka',\n",
       " 'America/St_Barthelemy',\n",
       " 'America/St_Johns',\n",
       " 'America/St_Kitts',\n",
       " 'America/St_Lucia',\n",
       " 'America/St_Thomas',\n",
       " 'America/St_Vincent',\n",
       " 'America/Swift_Current',\n",
       " 'America/Tegucigalpa',\n",
       " 'America/Thule',\n",
       " 'America/Tijuana',\n",
       " 'America/Toronto',\n",
       " 'America/Tortola',\n",
       " 'America/Vancouver',\n",
       " 'America/Whitehorse',\n",
       " 'America/Winnipeg',\n",
       " 'America/Yakutat',\n",
       " 'Antarctica/Casey',\n",
       " 'Antarctica/Davis',\n",
       " 'Antarctica/DumontDUrville',\n",
       " 'Antarctica/Macquarie',\n",
       " 'Antarctica/Mawson',\n",
       " 'Antarctica/McMurdo',\n",
       " 'Antarctica/Palmer',\n",
       " 'Antarctica/Rothera',\n",
       " 'Antarctica/Syowa',\n",
       " 'Antarctica/Troll',\n",
       " 'Antarctica/Vostok',\n",
       " 'Arctic/Longyearbyen',\n",
       " 'Asia/Aden',\n",
       " 'Asia/Almaty',\n",
       " 'Asia/Amman',\n",
       " 'Asia/Anadyr',\n",
       " 'Asia/Aqtau',\n",
       " 'Asia/Aqtobe',\n",
       " 'Asia/Ashgabat',\n",
       " 'Asia/Atyrau',\n",
       " 'Asia/Baghdad',\n",
       " 'Asia/Bahrain',\n",
       " 'Asia/Baku',\n",
       " 'Asia/Bangkok',\n",
       " 'Asia/Barnaul',\n",
       " 'Asia/Beirut',\n",
       " 'Asia/Bishkek',\n",
       " 'Asia/Brunei',\n",
       " 'Asia/Chita',\n",
       " 'Asia/Choibalsan',\n",
       " 'Asia/Colombo',\n",
       " 'Asia/Damascus',\n",
       " 'Asia/Dhaka',\n",
       " 'Asia/Dili',\n",
       " 'Asia/Dubai',\n",
       " 'Asia/Dushanbe',\n",
       " 'Asia/Famagusta',\n",
       " 'Asia/Gaza',\n",
       " 'Asia/Hebron',\n",
       " 'Asia/Ho_Chi_Minh',\n",
       " 'Asia/Hong_Kong',\n",
       " 'Asia/Hovd',\n",
       " 'Asia/Irkutsk',\n",
       " 'Asia/Jakarta',\n",
       " 'Asia/Jayapura',\n",
       " 'Asia/Jerusalem',\n",
       " 'Asia/Kabul',\n",
       " 'Asia/Kamchatka',\n",
       " 'Asia/Karachi',\n",
       " 'Asia/Kathmandu',\n",
       " 'Asia/Khandyga',\n",
       " 'Asia/Kolkata',\n",
       " 'Asia/Krasnoyarsk',\n",
       " 'Asia/Kuala_Lumpur',\n",
       " 'Asia/Kuching',\n",
       " 'Asia/Kuwait',\n",
       " 'Asia/Macau',\n",
       " 'Asia/Magadan',\n",
       " 'Asia/Makassar',\n",
       " 'Asia/Manila',\n",
       " 'Asia/Muscat',\n",
       " 'Asia/Nicosia',\n",
       " 'Asia/Novokuznetsk',\n",
       " 'Asia/Novosibirsk',\n",
       " 'Asia/Omsk',\n",
       " 'Asia/Oral',\n",
       " 'Asia/Phnom_Penh',\n",
       " 'Asia/Pontianak',\n",
       " 'Asia/Pyongyang',\n",
       " 'Asia/Qatar',\n",
       " 'Asia/Qostanay',\n",
       " 'Asia/Qyzylorda',\n",
       " 'Asia/Riyadh',\n",
       " 'Asia/Sakhalin',\n",
       " 'Asia/Samarkand',\n",
       " 'Asia/Seoul',\n",
       " 'Asia/Shanghai',\n",
       " 'Asia/Singapore',\n",
       " 'Asia/Srednekolymsk',\n",
       " 'Asia/Taipei',\n",
       " 'Asia/Tashkent',\n",
       " 'Asia/Tbilisi',\n",
       " 'Asia/Tehran',\n",
       " 'Asia/Thimphu',\n",
       " 'Asia/Tokyo',\n",
       " 'Asia/Tomsk',\n",
       " 'Asia/Ulaanbaatar',\n",
       " 'Asia/Urumqi',\n",
       " 'Asia/Ust-Nera',\n",
       " 'Asia/Vientiane',\n",
       " 'Asia/Vladivostok',\n",
       " 'Asia/Yakutsk',\n",
       " 'Asia/Yangon',\n",
       " 'Asia/Yekaterinburg',\n",
       " 'Asia/Yerevan',\n",
       " 'Atlantic/Azores',\n",
       " 'Atlantic/Bermuda',\n",
       " 'Atlantic/Canary',\n",
       " 'Atlantic/Cape_Verde',\n",
       " 'Atlantic/Faroe',\n",
       " 'Atlantic/Madeira',\n",
       " 'Atlantic/Reykjavik',\n",
       " 'Atlantic/South_Georgia',\n",
       " 'Atlantic/St_Helena',\n",
       " 'Atlantic/Stanley',\n",
       " 'Australia/Adelaide',\n",
       " 'Australia/Brisbane',\n",
       " 'Australia/Broken_Hill',\n",
       " 'Australia/Darwin',\n",
       " 'Australia/Eucla',\n",
       " 'Australia/Hobart',\n",
       " 'Australia/Lindeman',\n",
       " 'Australia/Lord_Howe',\n",
       " 'Australia/Melbourne',\n",
       " 'Australia/Perth',\n",
       " 'Australia/Sydney',\n",
       " 'Canada/Atlantic',\n",
       " 'Canada/Central',\n",
       " 'Canada/Eastern',\n",
       " 'Canada/Mountain',\n",
       " 'Canada/Newfoundland',\n",
       " 'Canada/Pacific',\n",
       " 'Europe/Amsterdam',\n",
       " 'Europe/Andorra',\n",
       " 'Europe/Astrakhan',\n",
       " 'Europe/Athens',\n",
       " 'Europe/Belgrade',\n",
       " 'Europe/Berlin',\n",
       " 'Europe/Bratislava',\n",
       " 'Europe/Brussels',\n",
       " 'Europe/Bucharest',\n",
       " 'Europe/Budapest',\n",
       " 'Europe/Busingen',\n",
       " 'Europe/Chisinau',\n",
       " 'Europe/Copenhagen',\n",
       " 'Europe/Dublin',\n",
       " 'Europe/Gibraltar',\n",
       " 'Europe/Guernsey',\n",
       " 'Europe/Helsinki',\n",
       " 'Europe/Isle_of_Man',\n",
       " 'Europe/Istanbul',\n",
       " 'Europe/Jersey',\n",
       " 'Europe/Kaliningrad',\n",
       " 'Europe/Kirov',\n",
       " 'Europe/Kyiv',\n",
       " 'Europe/Lisbon',\n",
       " 'Europe/Ljubljana',\n",
       " 'Europe/London',\n",
       " 'Europe/Luxembourg',\n",
       " 'Europe/Madrid',\n",
       " 'Europe/Malta',\n",
       " 'Europe/Mariehamn',\n",
       " 'Europe/Minsk',\n",
       " 'Europe/Monaco',\n",
       " 'Europe/Moscow',\n",
       " 'Europe/Oslo',\n",
       " 'Europe/Paris',\n",
       " 'Europe/Podgorica',\n",
       " 'Europe/Prague',\n",
       " 'Europe/Riga',\n",
       " 'Europe/Rome',\n",
       " 'Europe/Samara',\n",
       " 'Europe/San_Marino',\n",
       " 'Europe/Sarajevo',\n",
       " 'Europe/Saratov',\n",
       " 'Europe/Simferopol',\n",
       " 'Europe/Skopje',\n",
       " 'Europe/Sofia',\n",
       " 'Europe/Stockholm',\n",
       " 'Europe/Tallinn',\n",
       " 'Europe/Tirane',\n",
       " 'Europe/Ulyanovsk',\n",
       " 'Europe/Vaduz',\n",
       " 'Europe/Vatican',\n",
       " 'Europe/Vienna',\n",
       " 'Europe/Vilnius',\n",
       " 'Europe/Volgograd',\n",
       " 'Europe/Warsaw',\n",
       " 'Europe/Zagreb',\n",
       " 'Europe/Zurich',\n",
       " 'GMT',\n",
       " 'Indian/Antananarivo',\n",
       " 'Indian/Chagos',\n",
       " 'Indian/Christmas',\n",
       " 'Indian/Cocos',\n",
       " 'Indian/Comoro',\n",
       " 'Indian/Kerguelen',\n",
       " 'Indian/Mahe',\n",
       " 'Indian/Maldives',\n",
       " 'Indian/Mauritius',\n",
       " 'Indian/Mayotte',\n",
       " 'Indian/Reunion',\n",
       " 'Pacific/Apia',\n",
       " 'Pacific/Auckland',\n",
       " 'Pacific/Bougainville',\n",
       " 'Pacific/Chatham',\n",
       " 'Pacific/Chuuk',\n",
       " 'Pacific/Easter',\n",
       " 'Pacific/Efate',\n",
       " 'Pacific/Fakaofo',\n",
       " 'Pacific/Fiji',\n",
       " 'Pacific/Funafuti',\n",
       " 'Pacific/Galapagos',\n",
       " 'Pacific/Gambier',\n",
       " 'Pacific/Guadalcanal',\n",
       " 'Pacific/Guam',\n",
       " 'Pacific/Honolulu',\n",
       " 'Pacific/Kanton',\n",
       " 'Pacific/Kiritimati',\n",
       " 'Pacific/Kosrae',\n",
       " 'Pacific/Kwajalein',\n",
       " 'Pacific/Majuro',\n",
       " 'Pacific/Marquesas',\n",
       " 'Pacific/Midway',\n",
       " 'Pacific/Nauru',\n",
       " 'Pacific/Niue',\n",
       " 'Pacific/Norfolk',\n",
       " 'Pacific/Noumea',\n",
       " 'Pacific/Pago_Pago',\n",
       " 'Pacific/Palau',\n",
       " 'Pacific/Pitcairn',\n",
       " 'Pacific/Pohnpei',\n",
       " 'Pacific/Port_Moresby',\n",
       " 'Pacific/Rarotonga',\n",
       " 'Pacific/Saipan',\n",
       " 'Pacific/Tahiti',\n",
       " 'Pacific/Tarawa',\n",
       " 'Pacific/Tongatapu',\n",
       " 'Pacific/Wake',\n",
       " 'Pacific/Wallis',\n",
       " 'US/Alaska',\n",
       " 'US/Arizona',\n",
       " 'US/Central',\n",
       " 'US/Eastern',\n",
       " 'US/Hawaii',\n",
       " 'US/Mountain',\n",
       " 'US/Pacific',\n",
       " 'UTC']"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看常用的时区有哪些\n",
    "pytz.common_timezones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "6a1df26a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01 00:00:00+00:00     99\n",
       "2030-03-02 00:00:00+00:00     66\n",
       "2030-03-03 00:00:00+00:00    216\n",
       "2030-03-04 00:00:00+00:00    177\n",
       "2030-03-05 00:00:00+00:00    227\n",
       "                            ... \n",
       "2031-02-24 00:00:00+00:00    397\n",
       "2031-02-25 00:00:00+00:00    260\n",
       "2031-02-26 00:00:00+00:00    121\n",
       "2031-02-27 00:00:00+00:00     78\n",
       "2031-02-28 00:00:00+00:00     93\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时区表示\n",
    "ts = ts.tz_localize(tz='UTC')\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "3f040657",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2030-03-01 08:00:00+08:00    322\n",
       "2030-03-02 08:00:00+08:00    112\n",
       "2030-03-03 08:00:00+08:00    323\n",
       "2030-03-04 08:00:00+08:00    471\n",
       "2030-03-05 08:00:00+08:00    316\n",
       "                            ... \n",
       "2031-02-24 08:00:00+08:00    493\n",
       "2031-02-25 08:00:00+08:00    410\n",
       "2031-02-26 08:00:00+08:00    192\n",
       "2031-02-27 08:00:00+08:00    443\n",
       "2031-02-28 08:00:00+08:00    105\n",
       "Freq: D, Length: 365, dtype: int32"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时区转换\n",
    "ts = ts.tz_convert(tz='Asia/ShangHai')\n",
    "ts"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
